package net.huashitong.ssydt.user.util;


import net.huashitong.ssydt.security.utils.MD5;

/**
 * 验证码工具类
 *
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.10.10
 */
public class VerificationCodeUtils {

    private VerificationCodeUtils() {
    }

    /**
     * 生成token
     *
     * @param mobile
     * @return
     */
    public static String getToken(String mobile, String randomStr) {
        String str = String.format("%shst@2017%s", mobile, randomStr);
        return MD5.md5(str);
    }

    /**
     * 校验Token是否合法
     *
     * @param mobile
     * @param token
     * @return 如果合法返回 true
     */
    public static boolean verificationToken(String mobile, String token, String randomStr) {
        String serverToken = getToken(mobile, randomStr);
        return serverToken.equals(token);
    }

    /**
     * 检验签名
     *
     * @param mobile
     * @param token
     * @param timestamp
     * @param sign
     * @return 如果签名正确返回 true
     */
    public static boolean verificationSign(String mobile, String token, String timestamp, String sign) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(token).append(mobile).append(timestamp).append("hst&2017");
        String serverSign = MD5.md5(stringBuilder.toString());
        return serverSign.equals(sign);
    }


}
